/*
	jumper : Copyright 2011 Dale J Williams

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package lads.jumper.states {
	
	import lads.jumper.entities.Actor;

	public class RunState implements IState {
		
		private const maxAcceleration:Number = 2;
		
		private var actor:Actor;
		private var speed:Number;
		private var acceleration:Number;
		
		public function RunState(actor:Actor, speed:Number=0) {
			this.actor = actor;
			this.speed = speed;
			acceleration = maxAcceleration;
		}
		
		public function update():void {
			if (actor.direction > 0 == speed > 0) {
				actor.changeAnimation("run");
				acceleration *= 0.9;
			} else {
				actor.changeAnimation("skid");
				acceleration = maxAcceleration;
			}
			
			speed += acceleration * actor.direction;
			actor.x += speed;
			if (!actor.controller.move) {
				speed *= 0.7;
				if (speed < 5) {
					actor.changeAnimation("walk");
				}
				if (speed < 0.5 && speed > -0.5) {
					actor.state.changeState(new StandState(actor));
				}
			}
		}
		
	}

}